package com.et.drive.info

import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.convert.IMachineIdInfo
import com.et.machine.convert.machineIdInfo
import com.et.util.toHexString


data class MachineIdInfo(
    var MCCMIdentificationCode: String,
    var nDrink2: Int,
    var nFood2: Int,
    var nFood3: Int,
    var nFood4: Int,
    var ignored: String?= null,
    var minDenomination : Int? = null,
    var decimalPlaces : Int?= null,
    var coinDenomination: String?= null,
    var notesDenomination: String?= null,
    var machineVersion: String?= null

    ) {
    override fun toString(): String {
        return "MachineIdInfo(MCCMIdentificationCode='$MCCMIdentificationCode', nDrink2=$nDrink2, nFood2=$nFood2, nFood3=$nFood3, nFood4=$nFood4, ignored='$ignored', minDenomination=$minDenomination, decimalPlaces=$decimalPlaces, coinDenomination='$coinDenomination', notesDenomination='$notesDenomination', machineVersion='$machineVersion')"
    }
}

object IMachineIdInfoImpl : IMachineIdInfo {
    private var list: HashSet<((MachineIdInfo) -> Unit)> = HashSet()

    override fun observer(_callback: (MachineIdInfo) -> Unit) {
        list.add(_callback)
    }


    fun machineIdInfoNotify(byteArray: ByteArray) {
        MachineIdInfo(
            MCCMIdentificationCode = byteArray.toHexString(0, 12),
            nDrink2 = byteArray[12].toInt(),
            nFood2 = byteArray[13].toInt(),
            nFood3 = byteArray[14].toInt(),
            nFood4 = byteArray[15].toInt(),
            ignored = byteArray.toHexString(16,5),
            minDenomination = byteArray[21].toInt(),
            decimalPlaces = byteArray[22].toInt(),
            coinDenomination = byteArray.toHexString(23,6),
            notesDenomination = byteArray.toHexString(29,6),
            machineVersion = byteArray.toHexString(35,3)

        ).also { info ->
            machineIdInfo = info
            ELog.d(TAGS.machine, "Machine ID Info $info")
            if (list.isNotEmpty()) {
                list.forEach {
                    it(info)
                }
            }
        }
    }
}


